package com.emapp.lccode.fragment

import android.os.Bundle
import android.provider.Settings
import androidx.lifecycle.Observer
import com.emapp.lccode.R
import com.emapp.lccode.activity.MyOrderKtActivity
import com.emapp.lccode.adapter.HomeTypeKTAdapter
import com.emapp.lccode.base.BaseApplication
import com.emapp.lccode.base.BaseFragment1
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.HomeType
import com.emapp.lccode.bean.UserKt
import com.emapp.lccode.databinding.FragmentHomeBinding
import com.emapp.lccode.mode.Main2ViewModel
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.grid
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.toStartActivity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.hjq.toast.Toaster
import com.kingja.loadsir.core.LoadService
import com.luck.picture.lib.thread.PictureThreadUtils.runOnUiThread
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.ext.parseState
import org.greenrobot.eventbus.EventBus

class HomeKTFragment : BaseFragment1<Main2ViewModel, FragmentHomeBinding>() {

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>
    var types: ArrayList<HomeType> = arrayListOf()
    var user: UserKt? = null
    private val homeTypeAdapter: HomeTypeKTAdapter by lazy { HomeTypeKTAdapter() }
    override fun initView(savedInstanceState: Bundle?) {
//        types.add(HomeType(R.drawable.home_gongdan, "工单管理"))
////        types.add(HomeType(R.drawable.home_shangbao, "问题上报"))
//        types.add(HomeType(R.drawable.home_guanli, "设施设备管理"))
//        types.add(HomeType(R.drawable.home_check, "巡检日志"))
//        types.add(HomeType(R.drawable.home_yongche, "用车申请"))
//        types.add(HomeType(R.drawable.home_yongche2, "用车归还"))
//        types.add(HomeType(R.drawable.home_project, "项目地图"))
//        types.add(HomeType(R.drawable.home_myproject, "我的工程"))
        // types.add(HomeType(R.drawable.home_all, "全部"))
        user = BaseApplication.instance.getUser()

        if (user?.isChecker() == true){
            types.add(HomeType(R.drawable.home_gongdan, "工单管理"))
            types.add(HomeType(R.drawable.home_guanli, "设施设备管理"))
            types.add(HomeType(R.drawable.home_check, "巡检日志"))
            mViewBind.tvTip.text="欢迎巡检人员使用高标监管app"
        }else{
            types.add(HomeType(R.drawable.home_project, "项目地图"))
            types.add(HomeType(R.drawable.home_myproject, "我的工程"))
            mViewBind.tvTip.text="欢迎监理人员使用高标监管app"
        }
        mViewBind.rvType.run {
            grid(4)
            adapter = homeTypeAdapter
        }
        homeTypeAdapter.submitList(types)
    }

    /**
     * 懒加载
     */
    override fun lazyLoadData() {
        //  loadsir.showLoading()
        mViewModel.getGongdan()
//        XXPermissions.with(this)
//            .permission(Permission.READ_PHONE_STATE)
//            .request(object : OnPermissionCallback {
//                override fun onGranted(permissions: List<String>, all: Boolean) {
//                    val androidId: String = Settings.Secure.getString(appContext.contentResolver, Settings.Secure.ANDROID_ID)
//                    log_e("android----"+androidId)
//                }
//
//                override fun onDenied(permissions: List<String>, never: Boolean) {}
//            })
    }

    override fun createObserver() {
        mViewModel.run {
            gongdan.observe(viewLifecycleOwner, Observer { resultState ->
                parseState(resultState, {
                    mViewBind.tvDai.text = (it[0].publish.toString())
                    //mViewBind.tvZhong.text = (it[0].pending_processing.toString())
                    mViewBind.tvYi.text = (it[0].processed.toString())
                    getDevice()
                }, {
                    if (it.errorMsg.contains("token")) {
                        MmkvUtil.putBoolean("isAutoLogin", false)
                        EventBus.getDefault().post(EventBusModel(EventBusConfig.FINISH))
                    }
                })
            })
//            collectStreamData { data ->
//                runOnUiThread {
//                    log_e("ceshi----"+data.toString())
//                }
//            }

            device.observe(viewLifecycleOwner) {
                it.forEach { home ->
                    when (home.device_status) {
                        0 -> {
                            mViewBind.tvZhengchang.text = home.total.toString()
                        }

                        1 -> {
                            mViewBind.tvGuzhang.text = home.total.toString()
                        }

                        2 -> {
                            mViewBind.tvWeixiu.text = home.total.toString()
                        }

                        3 -> {
                            mViewBind.tvDiushi.text = home.total.toString()
                        }
                    }
                }
            }
            test.observe(viewLifecycleOwner, Observer { resultState ->
                parseState(resultState, {
                }, {
                    Toaster.show(it.errorMsg)
                })
            })
            img.observe(viewLifecycleOwner) {
                mViewBind.ivImage.setImageBitmap(it)
            }
        }
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(mViewBind.tvGongdan, mViewBind.tvDai) {
            when (it.id) {
                R.id.tv_gongdan -> {
                    toStartActivity(MyOrderKtActivity::class.java)
                }

                R.id.tv_dai -> {
                }
            }
        }
    }
}